home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlstyle_ < prev    next >
Text File  |  1995-04-18  |  7KB  |  234 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLSTYLE </TITLE>
  5. <h2>NAME</h2>
  6. perlstyle - Perl style guide
  7. <p><h2>DESCRIPTION</h2>
  8. <h3>Style</h3>
  9. Each programmer will, of course, have his or her own preferences in
  10. regards to formatting, but there are some general guidelines that will
  11. make your programs easier to read, understand, and maintain.  
  12. <p>Regarding aesthetics of code lay out, about the only thing Larry
  13. cares strongly about is that the closing curly brace of
  14. a multi-line BLOCK should line up with the keyword that started the construct.
  15. Beyond that, he has other preferences that aren't so strong:
  16. <p>
  17. <dl>
  18.  
  19. <dt><b><A NAME="perlcall_39">*</A></b>
  20. <dd>
  21. 4-column indent.
  22. <p></dd>
  23. <dt><B>*</B>
  24. <dd>
  25. Opening curly on same line as keyword, if possible, otherwise line up.
  26. <p></dd>
  27. <dt><B>*</B>
  28. <dd>
  29. Space before the opening curly of a multiline BLOCK.
  30. <p></dd>
  31. <dt><B>*</B>
  32. <dd>
  33. One-line BLOCK may be put on one line, including curlies.
  34. <p></dd>
  35. <dt><B>*</B>
  36. <dd>
  37. No space before the semicolon.
  38. <p></dd>
  39. <dt><B>*</B>
  40. <dd>
  41. Semicolon omitted in "short" one-line BLOCK.
  42. <p></dd>
  43. <dt><B>*</B>
  44. <dd>
  45. Space around most operators.
  46. <p></dd>
  47. <dt><B>*</B>
  48. <dd>
  49. Space around a "complex" subscript (inside brackets).
  50. <p></dd>
  51. <dt><B>*</B>
  52. <dd>
  53. Blank lines between chunks that do different things.
  54. <p></dd>
  55. <dt><B>*</B>
  56. <dd>
  57. Uncuddled elses.
  58. <p></dd>
  59. <dt><B>*</B>
  60. <dd>
  61. No space between function name and its opening paren.
  62. <p></dd>
  63. <dt><B>*</B>
  64. <dd>
  65. Space after each comma.
  66. <p></dd>
  67. <dt><B>*</B>
  68. <dd>
  69. Long lines broken after an operator (except "and" and "or").
  70. <p></dd>
  71. <dt><B>*</B>
  72. <dd>
  73. Space after last paren matching on current line.
  74. <p></dd>
  75. <dt><B>*</B>
  76. <dd>
  77. Line up corresponding items vertically.
  78. <p></dd>
  79. <dt><B>*</B>
  80. <dd>
  81. Omit redundant punctuation as long as clarity doesn't suffer.
  82. <p></dd>
  83.  
  84. </dl>
  85.  
  86. Larry has his reasons for each of these things, but he doen't claim that
  87. everyone else's mind works the same as his does.
  88. <p>Here are some other more substantive style issues to think about:
  89. <p>
  90. <dl>
  91. <dt><B>*</B>
  92. <dd>
  93. Just because you <I>CAN</I> do something a particular way doesn't mean that
  94. you <I>SHOULD</I> do it that way.  Perl is designed to give you several
  95. ways to do anything, so consider picking the most readable one.  For
  96. instance
  97. <p></dd>
  98. <pre>
  99.         open(FOO,$foo) || die "Can't open $foo: $!";
  100. </pre>
  101. is better than
  102. <p><pre>
  103.         die "Can't open $foo: $!" unless open(FOO,$foo);
  104. </pre>
  105. because the second way hides the main point of the statement in a
  106. modifier.  On the other hand
  107. <p><pre>
  108.         print "Starting analysis\n" if $verbose;
  109. </pre>
  110. is better than
  111. <p><pre>
  112.         $verbose && print "Starting analysis\n";
  113. </pre>
  114. since the main point isn't whether the user typed 
  115. <A HREF="perlrun.html#perlrun_361">-v</A>
  116.  or not.
  117. <p>Similarly, just because an operator lets you assume default arguments
  118. doesn't mean that you have to make use of the defaults.  The defaults
  119. are there for lazy systems programmers writing one-shot programs.  If
  120. you want your program to be readable, consider supplying the argument.
  121. <p>Along the same lines, just because you <I>CAN</I> omit parentheses in many
  122. places doesn't mean that you ought to:
  123. <p><pre>
  124.         return print reverse sort num values %array;
  125.         return print(reverse(sort num (values(%array))));
  126. </pre>
  127. When in doubt, parenthesize.  At the very least it will let some poor
  128. schmuck bounce on the % key in <B>vi</B>.
  129. <p>Even if you aren't in doubt, consider the mental welfare of the person
  130. who has to maintain the code after you, and who will probably put
  131. parens in the wrong place.
  132. <p><dt><B>*</B>
  133. <dd>
  134. Don't go through silly contortions to exit a loop at the top or the
  135. bottom, when Perl provides the 
  136. <A HREF="perlfunc.html#perlfunc_161">last</A>
  137.  operator so you can exit in
  138. the middle.  Just "outdent" it a little to make it more visible:
  139. <p></dd>
  140. <pre>
  141.         LINE:
  142.         for (;;) {
  143.             statements;
  144.           last LINE if $foo;
  145.             next LINE if /^#/;
  146.             statements;
  147.         }
  148. </pre>
  149. <dt><B>*</B>
  150. <dd>
  151. Don't be afraid to use loop labels--they're there to enhance
  152. readability as well as to allow multi-level loop breaks.  See the
  153. previous example.
  154. <p></dd>
  155. <dt><B>*</B>
  156. <dd>
  157. For portability, when using features that may not be implemented on
  158. every machine, test the construct in an eval to see if it fails.  If
  159. you know what version or patchlevel a particular feature was
  160. implemented, you can test 
  161. <A HREF="perlvar.html#perlvar_458">$]</A>
  162.  ($PERL_VERSION in <B>English</B>) to see if it
  163. will be there.  The <B>Config</B> module will also let you interrogate values
  164. determined by the <B>Configure</B> program when Perl was installed.
  165. <p></dd>
  166. <dt><B>*</B>
  167. <dd>
  168. Choose mnemonic identifiers.  If you can't remember what mnemonic means,
  169. you've got a problem.
  170. <p></dd>
  171. <dt><B>*</B>
  172. <dd>
  173. If you have a really hairy regular expression, use the <B>/x</B> modifier and
  174. put in some whitespace to make it look a little less like line noise.
  175. Don't use slash as a delimiter when your regexp has slashes or backslashes.
  176. <p></dd>
  177. <dt><B>*</B>
  178. <dd>
  179. Use the new "and" and "or" operators to avoid having to parenthesize
  180. list operators so much, and to reduce the incidence of punctuational
  181. operators like <B>&&</B> and <B>||</B>.  Call your subroutines as if they were
  182. functions or list operators to avoid excessive ampersands and parens.
  183. <p></dd>
  184. <dt><B>*</B>
  185. <dd>
  186. Use here documents instead of repeated print() statements.
  187. <p></dd>
  188. <dt><B>*</B>
  189. <dd>
  190. Line up corresponding things vertically, especially if it'd be too long
  191. to fit on one line anyway.  
  192. <p></dd>
  193. <pre>
  194.         $IDX = $ST_MTIME;       
  195.         $IDX = $ST_ATIME           if $opt_u; 
  196.         $IDX = $ST_CTIME           if $opt_c;     
  197.         $IDX = $ST_SIZE            if $opt_s;     
  198. </pre>
  199. <pre>
  200.         mkdir $tmpdir, 0700     or die "can't mkdir $tmpdir: $!";
  201.         chdir($tmpdir)      or die "can't chdir $tmpdir: $!";
  202.         mkdir 'tmp',   0777     or die "can't mkdir $tmpdir/tmp: $!";
  203. </pre>
  204. <dt><B>*</B>
  205. <dd>
  206. Line up your translations when it makes sense:
  207. <p></dd>
  208. <pre>
  209.         tr [abc]
  210.            [xyz];
  211. </pre>
  212. <dt><B>*</B>
  213. <dd>
  214. Think about reusability.  Why waste brainpower on a one-shot when you
  215. might want to do something like it again?  Consider generalizing your
  216. code.  Consider writing a module or object class.  Consider making your
  217. code run cleanly with <B>use strict</B> and 
  218. <A HREF="perlrun.html#perlrun_362">-w</A>
  219.  in effect.  Consider giving away
  220. your code.  Consider changing your whole world view.  Consider... oh,
  221. never mind.
  222. <p></dd>
  223. <dt><B>*</B>
  224. <dd>
  225. Be consistent.
  226. <p></dd>
  227. <dt><B>*</B>
  228. <dd>
  229. Be nice.
  230. <p></dd>
  231.  
  232. </dl>
  233.  
  234.